<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class AccountValidate extends CoreValidate {
	
	//constructor
	function AccountValidate() {
		
	}
	
	//methods
	function checkLogin($data_array) {
		//email address
		if( $this->_str_eq($data_array['field_email_address'], 0) ) {
			$this->_add('field_email_address', 'The Email is Null');
		} elseif( $this->_valid_email($data_array['field_email_address']) == false ) {
			$this->_add('field_email_address', 'The Email is invalid');
		}
		//password
		if( $this->_str_eq($data_array['field_password'], 0) ) {
			$this->_add('field_password', "The Password is Null");
		}
		//checkcode
		if( isset($data_array['field_checkcode']) && $data_array['field_checkcode'] != $_SESSION['checkcode'] ) {
			$this->_add('field_checkcode', "The Check code is Error");
		}
	}
	
	function checkRegister($data_array) {
		//username
		if( isset($data_array['field_username']) ) {
			if( $this->_str_eq($data_array['field_username'], 0) ) {
				$this->_add('field_username', 'The Username is Null');
			} elseif( $this->_str_lt($data_array['field_username'], 5) ) {
				$this->_add('field_username', 'The Username is less than 5 characters');
			} elseif( $this->_str_gt($data_array['field_username'], 20) ) {
				$this->_add('field_username', 'The Username is more than 20 characters');
			}
		}
		//firstname
		if( isset($data_array['field_firstname']) ) {
			if( $this->_str_eq($data_array['field_firstname'], 0) ) {
				$this->_add('field_firstname', 'The Firstname is Null');
			} elseif( $this->_str_lt($data_array['field_firstname'], 2) ) {
				$this->_add('field_firstname', 'The Firstname is less than 2 characters');
			} elseif( $this->_str_gt($data_array['field_firstname'], 20) ) {
				$this->_add('field_firstname', 'The Firstname is more than 20 characters');
			}
		}
		//lastname
		if( isset($data_array['field_lastname']) ) {
			if( $this->_str_eq($data_array['field_lastname'], 0) ) {
				$this->_add('field_lastname', 'The Lastname is Null');
			} elseif( $this->_str_lt($data_array['field_lastname'], 2) ) {
				$this->_add('field_lastname', 'The Lastname is less than 2 characters');
			} elseif( $this->_str_gt($data_array['field_lastname'], 20) ) {
				$this->_add('field_lastname', 'The Lastname is more than 20 characters');
			}
		}
		//email address
		if( $this->_str_eq($data_array['field_email_address'], 0) ) {
			$this->_add('field_email_address', 'The Email is Null');
		} elseif( $this->_valid_email($data_array['field_email_address']) == false ) {
			$this->_add('field_email_address', 'The Email is invalid');
		}
		//password
		if( $this->_str_eq($data_array['field_password'], 0) ) {
			$this->_add('field_password', "The Password is Null");
		} elseif( $this->_str_eq($data_array['field_password_confirm'], 0) ) {
			$this->_add('field_password_confirm', "The Password Confirm is Null");
		} elseif( $data_array['field_password'] !== $data_array['field_password_confirm'] ) {
			$this->_add('field_password_confirm', "The Password Confirm does not matche the Password");
		}
		//checkcode
		if( isset($data_array['field_checkcode']) && $data_array['field_checkcode'] != $_SESSION['checkcode'] ) {
			$this->_add('field_checkcode', "The Check code is Error");
		}
	}
	
	function checkRegisterAddress($data_array) {
		//contact name
		//if( $this->_str_eq($data_array['field_contactname'], 0) ) {
		//	$this->_add('field_contactname', 'The Name is Null');
		//} elseif( $this->_str_gt($data_array['field_contactname'], 20) ) {
		//	$this->_add('field_contactname', 'The Name is more than 20 characters');
		//}
		//first name
		if( $this->_str_eq($data_array['field_firstname'], 0) ) {
			$this->_add('field_firstname', 'The First Name is Null');
		} elseif( $this->_str_gt($data_array['field_firstname'], 20) ) {
			$this->_add('field_firstname', 'The First Name is more than 20 characters');
		}
		//last name
		if( $this->_str_eq($data_array['field_lastname'], 0) ) {
			$this->_add('field_lastname', 'The Last Name is Null');
		} elseif( $this->_str_gt($data_array['field_lastname'], 20) ) {
			$this->_add('field_lastname', 'The Last Name is more than 20 characters');
		}
		//email address
		if( $this->_str_eq($data_array['field_email_address'], 0) ) {
			$this->_add('field_email_address', 'The Email is Null');
		} elseif( $this->_valid_email($data_array['field_email_address']) == false ) {
			$this->_add('field_email_address', 'The Email is invalid');
		}
		//company
		if( $this->_str_eq($data_array['field_company'], 0) ) {
			$this->_add('field_company', 'The Company is Null');
		}
		//telephone
		if( $this->_str_eq($data_array['field_telephone'], 0) ) {
			$this->_add('field_telephone', 'The Telephone is Null');
		} elseif( $this->_str_lt($data_array['field_telephone'], 5) ) {
			$this->_add('field_telephone', 'The Telephone is less than 5 characters');
		} elseif( $this->_str_gt($data_array['field_telephone'], 20) ) {
			$this->_add('field_telephone', 'The Telephone is more than 20 characters');
		}
		//address
		if( $this->_str_eq($data_array['field_address'], 0) ) {
			$this->_add('field_address', 'The Address is Null');
		} elseif( $this->_str_lt($data_array['field_address'], 2) ) {
			$this->_add('field_address', 'The Address is less than 2 characters');
		} elseif( $this->_str_gt($data_array['field_address'], 200) ) {
			$this->_add('field_address', 'The Address is more than 200 characters');
		}
		//city
		if( $this->_str_eq($data_array['field_city'], 0) ) {
			$this->_add('field_city', 'The City is Null');
		} elseif( $this->_str_lt($data_array['field_city'], 2) ) {
			$this->_add('field_city', 'The City is less than 2 characters');
		} elseif( $this->_str_gt($data_array['field_city'], 200) ) {
			$this->_add('field_city', 'The City is more than 200 characters');
		}
		//postcode
		if( $this->_str_eq($data_array['field_postcode'], 0) ) {
			$this->_add('field_postcode', 'The Postcode is Null');
		} elseif( $this->_str_lt(str_replace('-', '', $data_array['field_postcode']), 5) || $this->_str_gt(str_replace('-', '', $data_array['field_postcode']), 9) ) {
			$this->_add('field_postcode', 'The Postcode is between 5 and 9 characters');
		} elseif( $this->_is_num(str_replace('-', '', $data_array['field_postcode'])) == false ) {
			$this->_add('field_postcode', 'The Postcode must be numeric');
		}
		//state/zone
		if( isset($data_array['field_zones_id']) && $data_array['field_zones_id'] > 0 ) {
			//
		} elseif( isset($data_array['field_zones_id']) && $this->_str_eq($data_array['field_state'], 0) ) {
			$this->_add('field_postcode', 'The State is Null');
		} elseif( isset($data_array['field_zones_id']) && $this->_str_lt($data_array['field_state'], 2) ) {
			$this->_add('field_state', 'The State is less than 2 characters');
		} elseif( isset($data_array['field_zones_id']) && $this->_str_gt($data_array['field_state'], 200) ) {
			$this->_add('field_state', 'The State is more than 200 characters');
		} else {
			$this->_add('field_state', 'Select or Enter a State');
		}
	}
	
	function checkRegisterWithAddress($data_array) {
		$this->checkRegister($data_array);
		$this->checkRegisterAddress($data_array);
	}
	
	function checkPassword($data_array) {
		//old_password
		if( $this->_str_eq($data_array['field_old_password'], 0) ) {
			$this->_add('field_old_password', 'The Old Password is Null');
		} elseif( $this->_str_lt($data_array['field_old_password'], 5) ) {
			$this->_add('field_old_password', 'The Old Password is less than 5 characters');
		} elseif( $this->_str_gt($data_array['field_old_password'], 20) ) {
			$this->_add('field_old_password', 'The Old Password is more than 20 characters');
		}
		//new_password
		if( $this->_str_eq($data_array['field_new_password'], 0) ) {
			$this->_add('field_new_password', 'The New Password is Null');
		} elseif( $data_array['field_new_password'] != $data_array['field_confirm_password'] ) {
			$this->_add('field_confirm_password', 'The Confirm Password does not match');
		}
	}
	
	function checkPasswordA($data_array) {
		if( $this->_str_eq($data_array['field_password'], 0) ) {
			$this->_add('field_password', 'The Password is Null');
		} elseif( $this->_str_lt($data_array['field_password'], 5) ) {
			$this->_add('field_password', 'The Password is less than 20 characters');
		} elseif( $this->_str_gt($data_array['field_password'], 20) ) {
			$this->_add('field_password', 'The Password is more than 20 characters');
		} elseif( $data_array['field_password'] != $data_array['field_password_confirm'] ) {
			$this->_add('field_password_confirm', 'The Confirm Password does not match');
		}
	}
	
	function checkAccount($data_array) {
		//username
		if( isset($data_array['field_username']) ) {
			if( $this->_str_eq($data_array['field_username'], 0) ) {
				$this->_add('field_username', 'The Name is Null');
			} elseif( $this->_str_lt($data_array['field_username'], 5) ) {
				$this->_add('field_username', 'The Name is less than 5 characters');
			} elseif( $this->_str_gt($data_array['field_username'], 20) ) {
				$this->_add('field_username', 'The Name is more than 20 characters');
			}
		}
		//firstname
		if( isset($data_array['field_firstname']) ) {
			if( $this->_str_eq($data_array['field_firstname'], 0) ) {
				$this->_add('field_firstname', 'The Firstname is Null');
			} elseif( $this->_str_lt($data_array['field_firstname'], 2) ) {
				$this->_add('field_firstname', 'The Firstname is less than 2 characters');
			} elseif( $this->_str_gt($data_array['field_firstname'], 20) ) {
				$this->_add('field_firstname', 'The Firstname is more than 20 characters');
			}
		}
		//lastname
		if( isset($data_array['field_lastname']) ) {
			if( $this->_str_eq($data_array['field_lastname'], 0) ) {
				$this->_add('field_lastname', 'The Lastname is Null');
			} elseif( $this->_str_lt($data_array['field_lastname'], 2) ) {
				$this->_add('field_lastname', 'The Lastname is less than 2 characters');
			} elseif( $this->_str_gt($data_array['field_lastname'], 20) ) {
				$this->_add('field_lastname', 'The Lastname is more than 20 characters');
			}
		}
		//email address
		if( $this->_str_eq($data_array['field_email_address'], 0) ) {
			$this->_add('field_email_address', 'The Email is Null');
		} elseif( $this->_valid_email($data_array['field_email_address']) == false ) {
			$this->_add('field_email_address', 'The Email is invalid');
		}
	}
	
	function checkAddress($data_array) {
		//contact name
		//if( $this->_str_eq($data_array['field_contactname'], 0) ) {
		//	$this->_add('field_contactname', 'The Contact Name is Null');
		//} elseif( $this->_str_gt($data_array['field_contactname'], 20) ) {
		//	$this->_add('field_contactname', 'The Contact Name is more than 20 characters');
		//}
		//first name
		if( $this->_str_eq($data_array['field_firstname'], 0) ) {
			$this->_add('field_firstname', 'The First Name is Null');
		} elseif( $this->_str_gt($data_array['field_firstname'], 20) ) {
			$this->_add('field_firstname', 'The First Name is more than 20 characters');
		}
		//last name
		if( $this->_str_eq($data_array['field_lastname'], 0) ) {
			$this->_add('field_lastname', 'The Last Name is Null');
		} elseif( $this->_str_gt($data_array['field_lastname'], 20) ) {
			$this->_add('field_lastname', 'The Last Name is more than 20 characters');
		}
		//email address
		if( $this->_str_eq($data_array['field_email_address'], 0) ) {
			$this->_add('field_email_address', 'The Email is Null');
		} elseif( $this->_valid_email($data_array['field_email_address']) == false ) {
			$this->_add('field_email_address', 'The Email is invalid');
		}
		//company
		if( $this->_str_eq($data_array['field_company'], 0) ) {
			$this->_add('field_company', 'The Company is Null');
		}
		//telephone
		if( $this->_str_eq($data_array['field_telephone'], 0) ) {
			$this->_add('field_telephone', 'The Telephone is Null');
		} elseif( $this->_str_lt($data_array['field_telephone'], 5) ) {
			$this->_add('field_telephone', 'The Telephone is less than 5 characters');
		} elseif( $this->_str_gt($data_array['field_telephone'], 20) ) {
			$this->_add('field_telephone', 'The Telephone is more than 20 characters');
		}
		//address
		if( $this->_str_eq($data_array['field_address'], 0) ) {
			$this->_add('field_address', 'The Address is Null');
		} elseif( $this->_str_lt($data_array['field_address'], 2) ) {
			$this->_add('field_address', 'The Address is less than 2 characters');
		} elseif( $this->_str_gt($data_array['field_address'], 200) ) {
			$this->_add('field_address', 'The Address is more than 200 characters');
		}
		//city
		if( $this->_str_eq($data_array['field_city'], 0) ) {
			$this->_add('field_city', 'The City is Null');
		} elseif( $this->_str_lt($data_array['field_city'], 2) ) {
			$this->_add('field_city', 'The City is less than 2 characters');
		} elseif( $this->_str_gt($data_array['field_city'], 200) ) {
			$this->_add('field_city', 'The City is more than 200 characters');
		}
		//postcode
		if( $this->_str_eq($data_array['field_postcode'], 0) ) {
			$this->_add('field_postcode', 'The Postcode is Null');
		} elseif( $this->_str_lt(str_replace('-', '', $data_array['field_postcode']), 5) || $this->_str_gt(str_replace('-', '', $data_array['field_postcode']), 9) ) {
			$this->_add('field_postcode', 'The Postcode is between 5 and 9 characters');
		} elseif( $this->_is_num(str_replace('-', '', $data_array['field_postcode'])) == false ) {
			$this->_add('field_postcode', 'The Postcode must be numeric');
		}
		//state/zone
		if( isset($data_array['field_zones_id']) && $data_array['field_zones_id'] > 0 ) {
			//
		} elseif( isset($data_array['field_zones_id']) && $this->_str_eq($data_array['field_state'], 0) ) {
			$this->_add('field_postcode', 'The State is Null');
		} elseif( isset($data_array['field_zones_id']) && $this->_str_lt($data_array['field_state'], 2) ) {
			$this->_add('field_state', 'The State is less than 2 characters');
		} elseif( isset($data_array['field_zones_id']) && $this->_str_gt($data_array['field_state'], 200) ) {
			$this->_add('field_state', 'The State is more than 200 characters');
		} else {
			$this->_add('field_state', 'Select or Enter a State');
		}
	}
	
	function checkAddressA($data_array, $fields) {
		if( !is_array($data_array) ) {
			return true;
		}
		foreach($data_array as $k => $v) {
			if( substr($k, 0, strlen('field_')) == 'field_' ) {
				$kb = substr($k, strlen('field_'));
			}
			if( (in_array($k, $fields) || in_array($kb, $fields)) && $this->_str_eq($data_array[$k], 0) == false ) {
				$this->_result_valid = false;
			}
		}
	}
	
	function checkEmail($email) {
		if( $this->_valid_email($email) == false ) {
			$this->_add('field_email_address', 'The Email is invalid');
		}
	}
}
//
?>